package replicantmanager;

import java.net.InetAddress;



public class ReplicantManager {
	
	
	public static void main(String[] args) throws Exception

	{
		System.out.println("start replicant manager");
		MyDatagramSocket _mySocket = null;
		String replicant1_ip = "132.205.45.214";
		int replicant1_port = 9890;
		
		String replicant2_ip = "132.205.45.214";
		int replicant2_port = 9891;
		
		String replicant3_ip = "132.205.45.214";
		int replicant3_port = 9892;
		
		try {
			 _mySocket = new MyDatagramSocket(8888);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		while (true) {
			
			String ret =  _mySocket.receiveMessage();
			System.out.println("receive from leader with message:"+ret);
			if(ret.contains("replicant1"))
			{
				System.out.println("send message to replicant1 side process to terminate replicant1 server");
				try {
					InetAddress receiverHost = InetAddress.getByName(replicant1_ip);
					_mySocket.sendMessage(receiverHost, replicant1_port, ret);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if(ret.contains("replicant2"))
			{
				System.out.println("send message to replicant2 side process to terminate replicant2 server");
				try {
					InetAddress receiverHost = InetAddress.getByName(replicant2_ip);
					_mySocket.sendMessage(receiverHost, replicant2_port, ret);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if(ret.contains("replicant3"))
			{
				System.out.println("send message to replicant3 side process to terminate replicant3 server");
				try {
					InetAddress receiverHost = InetAddress.getByName(replicant3_ip);
					_mySocket.sendMessage(receiverHost, replicant3_port, ret);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
		}
	}
	

}
